// pages/mch/order/sign.js
const App = getApp()
import Kits from '../../../utils/kits'
import Util from '../../../utils/util'
// import drawQrcode from '../../../miniprogram_npm/weapp-qrcode'
// const drawQrcode = require('../../../miniprogram_npm/weapp-qrcode')
Page({
  data: {
    init: false,
    order: {},
    con: {},
    certUrl: '',
    signUrl: '',
    certReq: false,
    signReq: false,
    timer: undefined,
    stateNum:0

  },
  onLoad(options) {
    this.execLoadOrder(options.oid)
  },
  onUnload() {
    if (this.data.timer) {
      clearTimeout(this.data.timer)
    }
  },
  execLoadOrder(oid) {
    App.doPost({
      uri: '/app/merchant/order/detail',
      body: {
        id: oid
      },
      fnSucc: resp => {
        resp.data.deposit.totalStr = Kits.fmtMoneyString(resp.data.deposit.total)
        resp.data.rent.totalStr = Kits.fmtMoneyString(resp.data.rent.total)
        resp.data.rent.paidStr = Kits.fmtMoneyString(resp.data.rent.paid)
        resp.data.rent.dayStr = Kits.fmtMoneyString(resp.data.rent.day)
        resp.data.totalStr = Kits.fmtMoneyString(resp.data.deposit.total + resp.data.rent.total)
        this.setData({
          init: true,
          order: resp.data,
        })
        this.execLoad()
      }
    })
  },
  execLoad() { 
    App.doPost({
      uri: '/app/merchant/order/contract',
      body: {
        id: this.data.order.id
      },
      fnSucc: resp => {
        this.setData({
          con: resp.data,
        })
        if (resp.data.signed) {
          wx.pub("ReloadOrderList", {})
        }

        // if (!resp.data.ready) {
        //   if (this.data.timer) {
        //     clearTimeout(this.data.timer)
        //   }
        //   this.data.timer = setTimeout(() => {
        //     this.execLoad()
        //   }, 4000);
        // } 
        else {
          this.execLoadQr()
        }
      }
    })
    this.setData({
      stateNum:1
    })
    setTimeout(()=>{
      this.setData({
        stateNum:0
      })
    },3000)
  },
  execLoadQr() {
    if (this.data.certUrl == '' && !this.data.con.customer.real.pass) {
      if (this.data.certReq) {
        return
      }
      this.data.certReq = true
      App.doPost({
        uri: '/app/merchant/order/customer/fdd',
        body: {
          id: this.data.order.id,
          type: 'Real'
        },
        fnSucc: resp => {
          this.setData({
            certUrl: resp.data.url,
          })
          this.data.certReq = false
        }
      })
    }
    if (this.data.signUrl == '' && this.data.con.customer.real.pass && this.data.con.customer.state != 2) {
      if (this.data.signReq) {
        return
      }
      this.data.signReq = true
      App.doPost({
        uri: '/app/merchant/order/customer/fdd',
        body: {
          id: this.data.order.id,
          type: 'Sign'
        },
        fnSucc: resp => {
          this.setData({
            signUrl: resp.data.url,
          })
          this.data.signReq = false
        }
      })
    }
  },
  toSendCertSms() {
    Util.Toast.fail(`暂未开放发送此短信`)
  },
  toSendSignSms() {
    Util.Toast.fail(`暂未开放发送此短信`)
  },
})